Docker Run vs Start vs Create: Difference Explained

您所在的位置:网站首页 docker start run 区别 Docker Run vs Start vs Create: Difference Explained

Docker Run vs Start vs Create: Difference Explained

#Docker Run vs Start vs Create: Difference Explained| 来源: 网络整理| 查看: 265

If you are new to Docker and learning it by following various tutorials, you might come across the terms like start docker container, run docker container or create docker container.

These terms are enough to confuse a docker beginner because all three docker commands seem similar.

In fact, it is specially difficult to tell the difference between docker run and docker start.

Isn鈥檛 running a container the same as starting it? Not really.

Let me explain it to you.

Difference between Docker run, Docker start and Docker create

Here鈥檚 what these commands do:

Docker create command creates a fresh new container from a docker image. However, it doesn鈥檛 run it immediately.

Docker start command will start any stopped container. If you used docker create command to create a container, you can start it with this command.

Docker run command is a combination of create and start as it creates a new container and starts it immediately. In fact, the docker run command can even pull an image from Docker Hub if it doesn鈥檛 find the mentioned image on your system.

Let鈥檚 see it with examples so that things are more clear to you.

Let鈥檚 see it with examples

Make sure that you have installed Docker if you want to follow the examples.

Let鈥檚 say that you download the Ubuntu image from Docker Hub using docker pull ubuntu command.

You can see all the available docker images on your system. I have only ubuntu in this example (to avoid confusion):

[email protected]:~$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 775349758637 5 weeks ago 64.2MB

Now, create a new docker container named container-1 with the docker create command:

[email protected]:~$ docker create --name container-1 ubuntu 2d9a8c190e6c9b3cbbc032a87762bfbc92f1dc0dd30abbe9bdb3ed7e74a6480f

You can see that it has created a new container. If you try to see all the running containers, you won鈥檛 see container-1 because though it was created, it was never started.

[email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

If you check all the containers, irrespective of whether they are running or not, you鈥檒l see that container-1 has Created status:

[email protected]:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2d9a8c190e6c ubuntu "/bin/bash" 18 seconds ago Created container-1

Now let鈥檚 use the docker run command to create and run a container named container-2:

[email protected]:~$ docker run -it -d --name container-2 ubuntu bash 13dc0f4226dc8d9d86e41d927c5616654d8263da2cc8c667aaa5b4dbd7f7e9b3

You can see that the container-2 is running as its status is Up:

[email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13dc0f4226dc ubuntu "bash" About a minute ago Up About a minute container-2

Let鈥檚 stop this running container:

[email protected]:~$ docker stop container-2 container-2 [email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [email protected]:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13dc0f4226dc ubuntu "bash" 2 minutes ago Exited (0) 28 seconds ago container-2 2d9a8c190e6c ubuntu "/bin/bash" 3 minutes ago Created container-1

Now that we have a stopped container, you can start it again using the docker start command:

[email protected]:~$ docker start container-2 container-2 [email protected]:~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 13dc0f4226dc ubuntu "bash" 2 minutes ago Up 2 seconds container-2

But what happens to the container-1 which was created using docker create command? You can start this container with docker start command and then use docker exec to run something specific with it.

I hope this article gave you a better understanding of docker run, docker start and docker create command. I'll advise learning about the container lifecycle for further understanding this topic.

Docker Commands for Managing Container LifecycleLearn the container life cycle concept. Also learn the Docker commands to manage each stage of the lifecycle of the containers.Linux HandbookDebdut Chakraborty

If you have questions or suggestion, please feel free to leave a comment below.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3